package cn.com.service.impl.ref;

import static cn.com.support.message.MessageCodeEnum.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.com.dal.dao.ref.RefUserRoleDAO;
import cn.com.dal.dataobject.ref.RefUserRoleDO;
import cn.com.service.ref.RefUserRoleService;
import cn.com.support.message.MessageResult;

/**
 * 系统用户/角色关系SERVICE实现类
	* @report
	* @Title RefUserRoleServiceImpl.java
	* @Since 2012-3-27
	* @Author Zoe
	* @Version 1.0.0 2012-3-27
 */
@Service
public class RefUserRoleServiceImpl implements RefUserRoleService {

	private Logger logger = LoggerFactory.getLogger(RefUserRoleServiceImpl.class);

	@Autowired
	private RefUserRoleDAO<RefUserRoleDO> userRoleDAO;
	
	@Transactional
	@Override
	public MessageResult<Long> insert(RefUserRoleDO userRoleDO) {
		MessageResult<Long> result =  new MessageResult<Long>(false, null, ERROR_INSERT);
		try {
			Long id = userRoleDAO.insert(userRoleDO);
			if (id != null && id > 0) {
				result = new MessageResult<Long>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.error(String.format(ERROR_INSERT.descript() + ":[%s]", userRoleDAO), e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Integer> updateById(RefUserRoleDO userRoleDO) {
		MessageResult<Integer> result =  new MessageResult<Integer>(false, null, ERROR_UPDATE);
		try {
			Integer id = userRoleDAO.updateById(userRoleDO);
			if (id != null && id > 0) {
				result = new MessageResult<Integer>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.error(String.format(ERROR_UPDATE.descript() + ":[%s]", userRoleDAO), e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Integer> deleteById(List<String> idLst) {
		MessageResult<Integer> result =  new MessageResult<Integer>(true, null, INFO_SUCCEED);
		Integer opId = null;
		RefUserRoleDO userRoleDO = null;
		try {
			for(String id : idLst){
				if (opId == null || opId <= 0) {
					result = new MessageResult<Integer>(false, opId, ERROR_DELETE);
					break;
				}
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.error(String.format(ERROR_DELETE.descript() + ":[%s]", idLst), e);
		}
		return result;
	}

	@Override
	public List<RefUserRoleDO> selectList(RefUserRoleDO userRoleDO) {
		List<RefUserRoleDO> result = null;
		try {
			result = userRoleDAO.selectList(userRoleDO);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]"), e);
		}
		return result;
	}

	@Override
	public RefUserRoleDO selectById(Long id) {
		RefUserRoleDO userRoleDO = null;
		try {
			userRoleDO = userRoleDAO.selectById(id);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]"), e);
		}
		return userRoleDO;
	}

}
